# ################################################################ #
# fortran.make                                                     #
#                                                                  #
# Common makefile for FORTRAN 77/90/95 programs                    #
#                                                                  #
# (c) 2002-2003 Wouter Caarls, Delft University of Technology      #
# ################################################################ #

include $(SHARE)/vars.make

# Variables ###################################################### #

SOURCES = $(shell ls *.[fF]90 *.[fF] 2> /dev/null)
OBJECTS = $(patsubst %.F90, %.o, $(patsubst %.F, %.o, $(patsubst %.f90, %.o, $(patsubst %.f, %.o, $(SOURCES)))))
MAKEFILE_DEPS = Makefile $(SHARE)/vars.make $(SHARE)/include.make $(SHARE)/fortran.make $(SHARE)/fortran.depend

# Compilation #################################################### #

$(filter-out $(OVERRIDE_TARGETS), all): subdirs.all $(NAME)

$(filter-out $(OVERRIDE_TARGETS), $(NAME)): $(OBJECTS)
	$(IFC) $(FFLAGS) -o$@ $(OBJECTS) $(FLFLAGS)

$(filter-out $(OVERRIDE_TARGETS), %.o):: %.f $(MAKEFILE_DEPS)
	$(IFC) $(FFLAGS) -c $<
$(filter-out $(OVERRIDE_TARGETS), %.o):: %.f90 $(MAKEFILE_DEPS)
	$(IFC) $(FFLAGS) -c $<
$(filter-out $(OVERRIDE_TARGETS), %.o):: %.F $(MAKEFILE_DEPS)
	$(IFC) $(FFLAGS) -c $<
$(filter-out $(OVERRIDE_TARGETS), %.o):: %.F90 $(MAKEFILE_DEPS)
	$(IFC) $(FFLAGS) -c $<

# Administration ################################################# #

$(filter-out $(OVERRIDE_TARGETS), clean): subdirs.clean
	rm -rf core core.[0-9]* $(NAME) *~ *.o *.d *.i $(NAME)-$(MAJOR).$(MINOR).$(REVISION) $(NAME)-$(MAJOR).$(MINOR).$(REVISION).tar $(NAME)-$(MAJOR).$(MINOR).$(REVISION).tar.gz

$(filter-out $(OVERRIDE_TARGETS), install): subdirs.install $(NAME) $(BINARIES)
	install -d $(PREFIX)/bin
	install -pc $(NAME) $(PREFIX)/bin
ifdef BINARIES
	install -pc $(BINARIES) $(PREFIX)/bin
endif

$(filter-out $(OVERRIDE_TARGETS), uninstall): subdirs.uninstall
	rm -f $(PREFIX)/bin/$(NAME)
ifdef BINARIES
	rm -f $(BINARIES:%=$(PREFIX)/bin/%)
endif

# Distribution ################################################### #

$(filter-out $(OVERRIDE_TARGETS), subdist): clean subdirs.dist fortran.subdist;

$(filter-out $(OVERRIDE_TARGETS), fortran.subdist): force

	mkdir $(DISTDIR)
	cp `find . -maxdepth 1 -name "*.[fF]" -o -name "*.[fF]90"` $(MAKEFILE_DEPS) $(DISTDIR)
	cp $(MAKEFILE_DEPS) $(DISTDIR)/$(SHAREDISTDIR)
ifdef DISTFILES
	cp --parent $(DISTFILES) $(DISTDIR)
endif
ifdef BINARIES
	cp --parent $(BINARIES) $(DISTDIR)
endif
	sed -e "s/.*SHARE *=.*/SHARE = $(SEDSHAREDISTDIR)/" Makefile > $(DISTDIR)/Makefile

# Dependencies ################################################### #

$(filter-out $(OVERRIDE_TARGETS), %.d):: %.f
	$(SHARE)/fortran.depend $(IFC) $(FFLAGS) $*
$(filter-out $(OVERRIDE_TARGETS), %.d):: %.f90
	$(SHARE)/fortran.depend $(IFC) $(FFLAGS) $*
$(filter-out $(OVERRIDE_TARGETS), %.d):: %.F
	$(SHARE)/fortran.depend $(IFC) $(FFLAGS) $*
$(filter-out $(OVERRIDE_TARGETS), %.d):: %.F90
	$(SHARE)/fortran.depend $(IFC) $(FFLAGS) $*

ifeq ($(filter clean uninstall dist subdist undist distclean realclean, $(MAKECMDGOALS)),)
-include $(filter %.d, $(patsubst %.F90, %.d, $(patsubst %.F, %.d, $(patsubst %.f90, %.d, $(patsubst %.f, %.d, $(SOURCES))))))
endif
